package com.erp.programmer.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.erp.programmer.bean.CodeMsg;
import com.erp.programmer.domain.Role;
import com.erp.programmer.dto.PageDto;
import com.erp.programmer.dto.ResponseDto;
import com.erp.programmer.dto.RoleDto;
import com.erp.programmer.service.IRoleService;
import com.erp.programmer.utils.CommonUtil;
import com.erp.programmer.utils.CopyUtil;
import com.erp.programmer.utils.ValidateEntityUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 陈鑫
 * @since 2021-05-23
 */
@RestController
@RequestMapping("/role")
@CrossOrigin
public class RoleController {
    @Autowired
    IRoleService iRoleService;
    /**
     *获取全部角色信息
     */
    @PostMapping("/get_data")
    public ResponseDto<PageDto<RoleDto>> getAll(@RequestBody PageDto<RoleDto> pageDto) {
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        if("name".equals(pageDto.getSearchType()) && !CommonUtil.isEmpty(pageDto.getSearchContent())){
            // 如果是根据角色名称搜索
            queryWrapper.like("name",pageDto.getSearchContent());
        }
        if(pageDto.getPage() == null){
            pageDto.setPage(1);
        }
        pageDto.setSize(5);
        PageHelper.startPage(pageDto.getPage(), pageDto.getSize());
        List<Role> list = iRoleService.list(queryWrapper);
        List<RoleDto> roleDtos = CopyUtil.copyList(list, RoleDto.class);
        PageInfo<Role> pageInfo = new PageInfo<>(list);
        pageDto.setTotal(pageInfo.getTotal());
        pageDto.setList(roleDtos);
        return ResponseDto.success(pageDto);
    }
    /**
     *保存角色信息
     */
    @PostMapping("/save")
    public ResponseDto<PageDto<RoleDto>> saveRole(@RequestBody RoleDto roleDto) {
        if (roleDto == null) {
            return ResponseDto.errorByMsg(CodeMsg.DATA_ERROR);
        }
        //进行统一表单验证
        CodeMsg validate = ValidateEntityUtil.validate(roleDto);
        if(!validate.getCode().equals(CodeMsg.SUCCESS.getCode())){
            return ResponseDto.errorByMsg(validate);
        }
        Role role = CopyUtil.copy(roleDto, Role.class);
        if(CommonUtil.isEmpty(role.getId())){
            //插入
            role.setId(null);
            if (!iRoleService.save(role)) {
                return ResponseDto.errorByMsg(CodeMsg.USER_ADD_ERROR);
            }
            PageDto<RoleDto> pageDtoList = getAll(new PageDto<RoleDto>()).getData();
            ResponseDto<PageDto<RoleDto>> responseDto = getAll(pageDtoList);
            responseDto.setMsg("添加成功！");
            return responseDto;
        }else{
            //修改
            if (!iRoleService.updateById(role)) {
                return ResponseDto.errorByMsg(CodeMsg.USER_EDIT_ERROR);
            }
            PageDto<RoleDto> pageDtoList = getAll(new PageDto<RoleDto>()).getData();
            ResponseDto<PageDto<RoleDto>> responseDto = getAll(pageDtoList);
            responseDto.setMsg("修改成功！");
            return responseDto;
        }
    }


    /**
     *删除角色信息
     */
    @PostMapping("/delete")
    public ResponseDto<PageDto<RoleDto>> deleteRole(@RequestBody RoleDto roleDto) {
        if (roleDto==null || CommonUtil.isEmpty(roleDto.getId())) {
            return ResponseDto.errorByMsg(CodeMsg.DATA_ERROR);
        }
        if(!iRoleService.removeById(roleDto.getId())) {
            return ResponseDto.errorByMsg(CodeMsg.USER_DELETE_ERROR);
        }
        PageDto<RoleDto> pageDtoList = getAll(new PageDto<RoleDto>()).getData();
        ResponseDto<PageDto<RoleDto>> responseDto = getAll(pageDtoList);
        responseDto.setMsg("删除成功！");
        return responseDto;
    }


}

